//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package dao.impl;

import dao.UserDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.User;
import utils.JDBCUtils;

public class UserDaoImpl implements UserDao{
    public UserDaoImpl() {
    }

    public User getUserByName(String username) throws SQLException {
        User userinDb = null;
        String sql = "select * from t_user where username = ?";
        ResultSet rs = JDBCUtils.executeQuery(sql, new Object[]{username});

        while(rs.next()) {
            Integer id = rs.getInt("id");
            String uname = rs.getString("username");
            String password = rs.getString("password");
            String gender = rs.getString("gender");
            String phone = rs.getString("phone");
            System.out.println(uname);
            userinDb = new User();
            userinDb.setId(id);
            userinDb.setUsername(uname);
            userinDb.setPassword(password);
            userinDb.setGender(gender);
            userinDb.setPhone(phone);
        }

        return userinDb;
    }
    public ArrayList<User> getAllUser() {
        String sql = "select * from t_user";
        ResultSet rs = JDBCUtils.executeQuery(sql, new Object[0]);
        ArrayList<User> users = new ArrayList();

        try {
            while(rs.next()) {
                Integer id = rs.getInt("id");
                String uname = rs.getString("username");
                String password = rs.getString("password");
                String gender = rs.getString("gender");
                String phone = rs.getString("phone");
                User userinDb = new User();
                userinDb.setId(id);
                userinDb.setUsername(uname);
                userinDb.setPassword(password);
                userinDb.setGender(gender);
                userinDb.setPhone(phone);
                users.add(userinDb);
            }
        } catch (Exception var10) {
            var10.printStackTrace();
        }

        return users;
    }

    public int addUser(User user) {
        String sql = "insert into t_user(username,password) values (?,?)";
        return JDBCUtils.executeUpdate(sql, new Object[]{user.getUsername(), user.getPassword()});
    }
}
